
var Tip = Class.create({
	className : 'Tip',
	mousedown : {'state':false},
	
	/**
	 * 构造函数
	 *
	 * @access	public
	 */
	initialize: function() 
	{
		var titlebox = 'titlebox';
		this.titleboxLastNode = null;
		this.titleboxOffstX = 10;
		this.titleboxOffstY = 10;
		this.titleboxDv = $(titlebox);

		if (!this.titleboxDv)
		{
			this.titleboxDv = document.createElement("div")
			Element.extend(this.titleboxDv);
			document.body.appendChild(this.titleboxDv);
			this.titleboxDv.writeAttribute('id', titlebox);
		}

		this.titleboxDv.addClassName(titlebox);
		this.titleboxDv.setStyle({visibility: 'hidden'});

		$(document.body).observe('mousemove', this.showTooltip.bind(this));
		$(document.body).observe('mousedown', this.hideTooltip.bind(this));
		$(document.body).observe('mouseup', this.downMouseState.bind(this));
	},

	showTooltip: function(event)
	{

		var elt = event.element(); 

		//&& !$(elt).descendantOf('map_bg')
		//if (elt.id !='map_bg' && !elt.hasClassName('drag_title'))//disabled 会出错
		//{
			////event.stop();
		//}
		//else
		//{
		//}

		//if ($(document.body) == elt)
		//{
			//elt = null;
			//return false;
		//}

		if (Prototype.Browser.IE)
		{
			//if (document.getElementsByTagName('html').item(0) == elt)
			//{
				//alert(2)
				//elt = null;
				//return false;
			//}
		}

		if (elt)
		{
			try
			{
				var title_text = elt.readAttribute('title');

				if (title_text)
				{
					elt.writeAttribute('title', '');
					elt.writeAttribute('msg', title_text);
				}

				if (!this.mousedown.state)
				{
					if (!title_text || title_text == '') title_text = elt.readAttribute('msg');

					//向上查找
					/*
					try
					{
						if (!title_text)
						{
							elt = elt.up();
							title_text = elt.readAttribute('title');

							if (title_text)
							{
								elt.writeAttribute('title', '');
							}

							if (!title_text || title_text == '') title_text = elt.readAttribute('msg');
						}
					}
					catch(e)
					{
					}
					*/
					
					if (title_text && typeof(title_text)=='string')
					{
						var ev_coords = {x:0, y:0};

						ev_coords.x = event.pointerX();
						ev_coords.y = event.pointerY();

						if (this.titleboxLastNode != elt)
						{


							var title_width = elt.readAttribute('titlewidth');
							var title_offset = elt.readAttribute('titleoffset');

							if (typeof(title_width) == 'string' && title_width)
							{
								this.titleboxDv.style.width = title_width + 'px';
								var nowrap = ''
							}
							else
							{
								this.titleboxDv.style.width = document.all ? 'auto': '1px';
								var nowrap = ' nowrap'
							}

							this.titleboxDv.innerHTML = '<table cellpadding="0" cellspacing="0" align="center"><tr><td' + nowrap + '>' + title_text + '</td></tr></table>';
							this.titleboxDv.style.visibility = "visible"
						}

						this.titleboxLastNode = elt;

						var table = this.titleboxDv.getElementsByTagName('table').item(0);

						if (typeof(title_offset == 'string') && title_offset)
						{
							var i = title_offset.split(',');
							var title_offset_x = Math.round(i[0]);
							var title_offset_y = Math.round(i[1]);
							i = null;
						}
						else
						{
							var title_offset_x = this.titleboxOffstX;
							var title_offset_y = this.titleboxOffstY
						}

						if (ev_coords.x + title_offset_x > $(document.body).offsetWidth - table.offsetWidth)
						{
							var l = Math.max(title_offset_x, ev_coords.x - table.offsetWidth - title_offset_x)
						}
						else 
						{
							var l = ev_coords.x + title_offset_x
						}

						if (ev_coords.y + title_offset_y > $(document.body).offsetHeight - table.offsetHeight)
						{
							var t = Math.max(title_offset_y, ev_coords.y - table.offsetHeight - title_offset_y)
						} 
						else 
						{
							var t = ev_coords.y + title_offset_y;
						}

						this.titleboxDv.setStyle({'left': l + 'px', 'top': t + 'px'}); 



						ev_coords = null;
						title_width = null;
						title_offset = null;
						nowrap = null;

						table = null;
						title_offset_x = null;
						title_offset_y = null;
						l = null;
						t = null;

						//停止事件
						event.stop();
					}
					else
					{
						this.doHideTooltip();
					}

					title_text = null;

				}

			}
			catch(e) {}
		}

		elt = null;
	},
	
	hideTooltip: function(event)
	{
		//event.stop(); 
		//this.mousedown.state = true;

		this.doHideTooltip();

	},

	doHideTooltip: function()
	{
		if (typeof(this.titleboxDv) == 'object')
		{
			this.titleboxDv.style.visibility = "hidden";
			this.titleboxLastNode = null
		}
	},

	
	downMouseState: function(event)
	{
		//event.stop(); 
		this.mousedown.state = false;
	}
});

//var T;
//document.observe("dom:loaded", function()
//{
	//T = new Tip();
//});

/*
var toolTips = Class.create({
	className : 'Tip',

	initialize:function(delayDur){
		this.wrapperId = "toolTipsWrapper";
		this.show = false;
		this.xGap = 15;
		this.yGap = 15;
		this.opacity = 0.85;
		this.delayDur = delayDur || 0;
		this.init();
	},

	init:function() {
		this.insertWrapper();
		document.observe("mouseover", this.showToolTips.bind(this));
		document.observe("mouseout", this.hideToolTips.bind(this));
		document.observe("mousemove", this.moveToolTips.bind(this));
		document.observe('mousedown', this.hideToolTips.bind(this));
	},

	insertWrapper:function() {
		$(document.body).insert(new Element("div",{'id':this.wrapperId,'style':'position: absolute;display: none;width:auto;z-index: 10000;'}));
		$(this.wrapperId).setStyle({opacity:this.opacity});
	},

	showToolTips:function(e) {
		var el = e.element();
		var o = $(this.wrapperId);
		if(el.title){
			el.toolTip = el.title;
			el.title = "";
		};
		if(el.toolTip){
			o.update(el.toolTip).show.bind(o).delay(this.delayDur);
			this.show = true;
		}
	},

	hideToolTips:function(e) {
		var el = e.element();
		var o = $(this.wrapperId);
		o.setStyle({left:"-1000px",display:"none"})
		this.show = false;
	},

	moveToolTips:function(e) {
		if(this.show){
			var x = e.pointerX();
			var y = e.pointerY();
			var x1 = this.xGap;
			var y1 = this.yGap;
			var o = $(this.wrapperId);
			var dims = o.getDimensions();
			var oW = dims.width;
			var oH = dims.height;
			var docDims = document.viewport.getDimensions();
			var docW = docDims.width;
			var docH = docDims.height;
			var docScroll = document.viewport.getScrollOffsets();
			var docLeft = docScroll.left;
			var docTop = docScroll.top;
			
			//var posX=(x+x1+oW-docLeft>docW) ? docW+docLeft-oW : x+x1;
			//var posY=(y+y1+oH-docTop>docH) ? docH+docTop-oH : y+y1;

			var posX=(x+x1+oW-docLeft>docW) ? x-oW-x1 : x+x1;
			var posY=(y+y1+oH-docTop>docH) ? y-oH-y1 : y+y1;

			o.setStyle({
				left: posX+"px",
				top: posY+"px"
			})
		}
	}
});
*/
//document.observe("dom:loaded", function()
//{
	//new toolTips();
//});


